package com.zide.system.controller;

import com.zide.common.annotation.Log;
import com.zide.common.core.domain.CommonResult;
import com.zide.common.core.domain.PageResult;
import com.zide.common.core.domain.entity.SysDictType;
import com.zide.common.enums.BusinessType;
import com.zide.common.utils.poi.ExcelUtil;
import com.zide.system.domain.dict.vo.result.SysDictTypeResultVO;
import com.zide.system.domain.dict.vo.search.SysDictTypeSearchVO;
import com.zide.system.service.ISysDictTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import static com.zide.common.core.domain.CommonResult.*;

/**
 * 数据字典信息
 *
 * @author zide
 */
@Tag(name = "数据字典管理")
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @Operation(summary = "字典列表")
    @GetMapping("/list")
    public CommonResult list(SysDictTypeSearchVO searchVO) {
        PageResult<SysDictTypeResultVO> result = dictTypeService.searchPage(searchVO);
        return success(result);
    }

    @Operation(summary = "字典导出")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictType dictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    @Operation(summary = "查询字典类型详细")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable Long id) {
        return success(dictTypeService.selectDictTypeById(id));
    }

    @Operation(summary = "新增字典类型")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return getResult(dictTypeService.insertDictType(dict));
    }

    @Operation(summary = "修改字典类型")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@Validated @RequestBody SysDictType dict) {
        if (!dictTypeService.checkDictTypeUnique(dict)) {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return getResult(dictTypeService.updateDictType(dict));
    }

    @Operation(summary = "删除字典类型")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public CommonResult remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    @Operation(summary = "刷新字典缓存")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public CommonResult refreshCache() {
        dictTypeService.resetDictCache();
        return success();
    }

    @Operation(summary = "获取字典选择框列表")
    @GetMapping("/optionselect")
    public CommonResult optionselect() {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
